package com.cgbl.cg.vm.resource

import android.app.Application
import android.os.Bundle
import androidx.databinding.ObservableField
import api.ResourceServiceFactory
import com.blankj.utilcode.util.LogUtils
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.FilterBean
import com.robot.base.model.PageListBean
import com.robot.base.model.ResourceBean
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class ResourceMenuFraViewModel(application: Application) : GLBaseViewModel(application) {

    val listNull = ObservableField(true)

    val tag = ObservableField(true)
    val uc = UiChangeEvent()

    var type = ""
    var isFree = ""
    var maxPrice = ""
    var minPrice = ""
    var ms = ""
    var resourcesType = ""
    var wiringType = ""
    var sort = 0

    var pageNo = 1

    class UiChangeEvent {
        val refreshStatusEvent = SingleLiveEvent<Int>()
        val refreshDataEvent = SingleLiveEvent<List<ResourceBean>>()
        val refreshTabEvent = SingleLiveEvent<List<FilterBean.FilterChildBean>>()
    }

    val filterCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityWithBundle(
            AppConstants.Router.RESOURCE.A_FILTER_RESOURCE,
            Bundle().apply {
                putBoolean("finishType", true)
            }
        )
    })

    val leftTagCommand = BindingCommand<Any>(BindingAction {
        tag.set(true)
        sort = 0
    })

    val rightTagCommand = BindingCommand<Any>(BindingAction {
        tag.set(false)
        sort = 1
    })

    fun initData(it: FilterBean) {
        val list = arrayListOf<FilterBean.FilterChildBean>()
        isFree = it.isFree?.id ?: ""
        if (it.isFree?.id == "0") {
            maxPrice = it.isFree?.text?.split("-")?.get(1) ?: ""
            minPrice = it.isFree?.text?.split("-")?.get(0) ?: ""
        } else {
            maxPrice = ""
            minPrice = ""
        }
        ms = it.ms?.id ?: ""
        resourcesType = it.resourcesType?.id ?: ""
        wiringType = it.wiringType?.id ?: ""
        list.clear()
        if (it.isFree != null) {
            list.add(it.isFree!!)
        }
        if (it.ms != null) {
            list.add(it.ms!!)
        }
        if (it.wiringType != null) {
            list.add(it.wiringType!!)
        }
        if (it.resourcesType != null) {
            list.add(it.resourcesType!!)
        }
        uc.refreshTabEvent.postValue(list)
        listNull.set(list.isEmpty())
        refresh()
    }

    fun refresh() {
        pageNo = 1
        page()
    }

    fun load() {
        pageNo++
        page()
    }

    fun page() {
        ResourceServiceFactory.page(
            mapOf(
                "pageNo" to pageNo,
                "pageSize" to 10,
                "sort" to sort,
                "ms" to ms,
                "resourcesType" to resourcesType,
                "wiringType" to wiringType,
                "isAccount" to 0,
                "twoCategoryId" to type,
                "isFree" to isFree,
                "maxPrice" to maxPrice,
                "minPrice" to minPrice
            )
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<ResourceBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<ResourceBean>>) {
                uc.refreshStatusEvent.postValue(
                    if (t.data.total.toInt() < pageNo * 10) {
                        0
                    } else {
                        1
                    }
                )
                uc.refreshDataEvent.postValue(t.data.list)
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                uc.refreshStatusEvent.postValue(
                    0
                )
            }
        })
    }
}